<?php
/***********************************************************************
 * @package    IvanEngine
 * @subpackage Framework
 * @author     Ivan <ivanzx@msn.com>
 * @created    2010-05-23
 ***********************************************************************/
if(defined("IVANENGINE_FRAMEWORK_MVC_ACTION"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_FRAMEWORK_MVC_ACTION", true);

require_once (dirname(__FILE__).'/../Common.php');
require_once (M_CORE_PATH.'Response.php');
require_once (M_CORE_PATH.'Request.php');

abstract class CAction extends CObject 
{
    protected $m_plugin     = null;
    protected $m_winMgr     = null;
    protected $m_pluginMgr  = null;
    protected $m_context    = null;
    protected $m_varMap     = null;
    protected $m_layout     = null;
    protected $m_i18n       = null;
    
	public function __construct(CPlugin & $plugin)
	{
		parent::__construct();
        $this->m_plugin     = $plugin;
        $this->m_winMgr     = CWindowManager::Get();
        $this->m_pluginMgr  = CPluginManager::Get();
        $this->m_context    = CContext::Get();
        $this->m_varMap     = new CMap();
	}

	public function __destruct()
	{
        unset($this->m_i18n);
        unset($this->m_plugin);
        unset($this->m_varMap);
        unset($this->m_layout);
        unset($this->m_winMgr);
        unset($this->m_pluginMgr);
        unset($this->m_context);
		parent::__destruct();
	}
    
    public function _Initialize()
    {
        $this->m_i18n       = $this->m_plugin->GetI18N();
        $this->SetName($this->m_plugin->GetName());
        $this->SetLayout('Layout.php');
        $this->Set('Language', $this->m_i18n->GetArray());
        $this->Set('_L', $this->m_i18n->GetArray());
    }
    
    public function SetLayout($fileName)
    {
        if( $fileName && strlen($fileName) > 0 )
        {
            $fileName   = $this->m_plugin->GetPath().M_DS.$fileName;
            $fileName   = CFileHelper::CorrectPath($fileName);
            if( CFileHelper::IsExsit($fileName) ){
                $this->m_layout  = $fileName;
            }
        }else{
            $this->m_layout = null;
        }
    }
    
    public function & GetLayout()
    {
        return $this->m_layout;
    }
    
    public function & _GetVarMap()
    {
        return $this->m_varMap;
    }
    
    public function & GetPlugin()
    {
        return $this->m_plugin;
    }
    
    public function SetView(CView & $view)
    {
        $this->m_plugin->SetView($view);
    }
    
    public function & GetView()
    {
        return $this->m_plugin->GetView();
    }
    
    public function Set($key, $val)
    {
        if( $this->m_varMap->Has($key) )
        {
            $this->m_varMap->Set($key, $val);
        }else{
            $this->m_varMap->Add($key, $val);
        }
    }
    
    public function Get($key)
    {
        return $this->m_varMap->Get($key);
    }
    
    public function Remove($key)
    {
        $this->m_varMap->Remove($key);
    }
    
    public function PreExecuteAction($actionName, CRequest & $request)
    {
        return true;
    }
    
    public function PostExecuteAction($actionName, CRequest & $request)
    {
        return true;
    }
    
    public function PreRender($actionName, $request)
    {
        return true;
    }
    
    public function PostRender($actionName, $request)
    {
        return true;
    }
    
    abstract public function DoIndex(CRequest & $request);
}
?>